package csc.training.jsf2.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import csc.training.jsf2.dao.MailSystemDao;
import csc.training.jsf2.entity.EmailInfo;


/**
 * 
 * @author hdang20
 *
 */
@ManagedBean
@SessionScoped
public class MailManageServiceImpl implements MailManageService, Serializable{
	private static final long serialVersionUID = 773499474747076039L;
	@ManagedProperty(value="#{mailSystemDaoImpl}")
	private MailSystemDao mailSystemDao;
	
	public List<EmailInfo> getEmails(){
		return mailSystemDao.getEmails();
	}
	
	public List<EmailInfo> getInboxMails(String contentForSearch){
		List<EmailInfo> emails = new ArrayList<EmailInfo>();
		for (EmailInfo emailInfo : getEmails()) {
			if(contentForSearch != null && !contentForSearch.equals("")){
				if((emailInfo.getStatus().equals(EmailInfo.READ) 
						|| emailInfo.getStatus().equals(EmailInfo.NOT_YET_READ))
						&& emailInfo.getSender().toUpperCase().indexOf(contentForSearch.toUpperCase())>=0){
					emails.add(emailInfo);
				}
			}else{
				if((emailInfo.getStatus().equals(EmailInfo.READ) 
						|| emailInfo.getStatus().equals(EmailInfo.NOT_YET_READ))){
					emails.add(emailInfo);
				}
			}
		}
		return emails;
	}

	public List<EmailInfo> getSentMails(String contentForSearch){
		List<EmailInfo> emails = new ArrayList<EmailInfo>();
		for (EmailInfo emailInfo : getEmails()) {
			if(contentForSearch != null && !contentForSearch.equals("")){
				if(emailInfo.getStatus().equals(EmailInfo.SENT) 
						&& emailInfo.getSender().equals(contentForSearch)){
					emails.add(emailInfo);
				}
			}else{
				if(emailInfo.getStatus().equals(EmailInfo.SENT)){
					emails.add(emailInfo);
				}
			}
		}
		return emails;
	}
	public List<EmailInfo> getDeletedMails(String contentForSearch){
		List<EmailInfo> emails = new ArrayList<EmailInfo>();
		for (EmailInfo emailInfo : getEmails()) {
			if(contentForSearch != null && !contentForSearch.equals("")){
				if(emailInfo.getStatus().equals(EmailInfo.DELETED) 
						&& emailInfo.getSender().equals(contentForSearch)){
					emails.add(emailInfo);
				}
			}else{
				if(emailInfo.getStatus().equals(EmailInfo.DELETED)){
					emails.add(emailInfo);
				}
			}
		}
		return emails;
	}
	
	public void setMailSystemDao(MailSystemDao mailSystemDao) {
		this.mailSystemDao = mailSystemDao;
	}
	
}
